
import { observable, action } from 'mobx';
import { Button } from 'antd';
import { WeaForm } from 'comsMobx';
import BaseListStore from './common/BaseListStore';
import * as API_TODO from '../apis/todoApi';

/**
 * 待办公文页面 数据对象
 */
export default class TodoStore extends BaseListStore {
  @observable showBatchBtn = true;
  locationquery = {};

  @action
  setShowBatchBtn = (showBatchBtn) => {
    this.showBatchBtn = showBatchBtn;
  }

  constructor(params = {}) {
    super();
    // this.setTopTitle(this.getLabel2(384728,"待办公文"));
    this.setPageTitle(params);
    // 默认是新的代办公文 可以通过isNew=false || true 改变新旧列表
    if (Object.prototype.hasOwnProperty.call(params, 'isNew') && (params.isNew == true || params.isNew == 'true')) {
      this.tabComponentParams = {
        getDataList: param => this.doSearch(param),
        searchType: ['base', 'advanced'],
        getFormUrl: '/api/odoc/odocTodoList/getCondition',
        tabUrl: '/api/odoc/odocTodoList/getTodoCount',
        tabParams: params,
        searchParams: params,
        tabKey: 'officalType',
        baseSearchName: 'requestname',
        moreTimeGetCondition: true,
      };
      if (Object.prototype.hasOwnProperty.call(params, 'officalType')) {
        this.tabComponentParams.tabUrl = undefined;
        this.tabComponentParams.tabKey = undefined;
      }
      this.showNewSearch = true;
    } else {
      this.API_TABCOUNT = '/api/odoc/odocTodoList/getTodoCount';
      this.showNewSearch = false;
    }
    this.locationquery = params;
  }

  // 构建tab页
  buildTabs(module, params = {}) {
    params = { ...params, ...this.locationquery };
    super.buildTabs(module, params);
  }

  getSearchsBaseValue() {
    return this.searchStore.form.getFormParams().requestname;
  }

  setSearchsBaseValue(v) {
    this.searchStore.form.updateFields({
      requestname: {
        value: v,
      },
    });
    this.searchStore.isSearched();
  }

  getTopBtns() {
    const btns = [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@fythy3@bacthSubmit`} type="ghost" disabled={this.showBatchBtn}>{this.getLabel2('17598', '批量提交')}</Button>,
    ];

    return (btns);
  }

  handleSelectedRowsChange(selectedRows) {
    this.setSelectedRows(selectedRows);
    if (this.selectedRows.length > 0) {
      this.setShowBatchBtn(false);
    } else {
      this.setShowBatchBtn(true);
    }
  }

  @action.bound
  doSearch(params = {}) {
    let param_todoList = this.searchStore.getFormParams();
    param_todoList = { officalType: 1, ...param_todoList, ...this.searchStore.otherSearchParams, ...this.locationquery, ...params };
    API_TODO.getTodoList(param_todoList).then((data) => {
      this.searchStore.tableStore.getDatas(data.generateParams.sessionkey, params.current || 1);
      // this.dataKey = data.generateParams.sessionkey;
      // this.loading = false;
    });
    this.searchStore.setShowSearchAd(false);
    return this;
  }

  buildSearchCondition(params = {}) {
    Object.assign(params, params, this.searchStore.otherSearchParams, this.locationquery);
    API_TODO.getCondition(params).then((data) => {
      this.searchStore.form = new WeaForm();
      this.searchStore.setSearchCondition(data);
      const conditioninfo = data.conditions || data.condition;
      this.searchStore.form.initFormFields(conditioninfo);
      this.doSearch(params);
    });
  }

  resetForm() {
    this.searchStore.resetSearchForm();
    // this.searchStore.form.resetForm();
    this.setSearchsBaseValue('');
    // this.searchStore.isSearched();
  }

  getColumns = (columns) => {
    let newColumns = columns.map(column =>
      // if(column.dataIndex == "odoctype"){
      //   return {
      //     ...column, render: (text, record, index) => {
      //       return (<p  class="wea-odoctype-labelp" title={record.odoctypespan}>{record.odoctypespan}</p>);
      //     }
      //   }
      // } else {
      column,
      // }
    );

    return newColumns;
  }

  setPageTitle(params) {
    if (params.type == 'todo') {
      this.setTopTitle(this.getLabel2(384728, '待办公文'));
      params.officalType == '1' && this.setTopTitle(this.getLabel2('33769', '待办发文'));
      params.officalType == '2' && this.setTopTitle(this.getLabel2('33787', '待办收文'));
      params.officalType == '3' && this.setTopTitle(this.getLabel2('382113', '待办签报'));
    }
    if (params.type == 'done') {
      this.setTopTitle(this.getLabel2(507087, '已办公文'));
      params.officalType == '1' && this.setTopTitle(this.getLabel2('33770', '已办发文'));
      params.officalType == '2' && this.setTopTitle(this.getLabel2('33789', '已办收文'));
      params.officalType == '3' && this.setTopTitle(this.getLabel2('382115', '已办签报'));
    }
    if (params.type == 'toview') {
      this.setTopTitle(this.getLabel2(509711, '待阅公文'));
      params.officalType == '1' && this.setTopTitle(this.getLabel2('509714', '待阅发文'));
      params.officalType == '2' && this.setTopTitle(this.getLabel2('509715', '待阅收文'));
      params.officalType == '3' && this.setTopTitle(this.getLabel2('509716', '待阅签报'));
    }
    if (params.type == 'viewed') {
      this.setTopTitle(this.getLabel2(509710, '已阅公文'));
      params.officalType == '1' && this.setTopTitle(this.getLabel2('509717', '已阅发文'));
      params.officalType == '2' && this.setTopTitle(this.getLabel2('509718', '已阅收文'));
      params.officalType == '3' && this.setTopTitle(this.getLabel2('509719', '已阅签报'));
    }
  }
}
